﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using System.Text;
using System.Reflection;

namespace CRM.Web.ComplaintCaseManage
{
    public partial class Modify : Common.PageBase
    {
        /// <summary>
        /// Gets or sets the case status.
        /// </summary>
        /// <value>
        /// The case status.
        /// </value>
        private string CaseStatus
        {
            get { return (ViewState["CaseStatus"] == null) ? "" : ViewState["CaseStatus"].ToString(); }
            set { ViewState["CaseStatus"] = value; }
        }

        #region 业务访问类
        /// <summary>
        /// 投诉工单
        /// </summary>
        T_COMPLAINTCASE_BLL bll = new T_COMPLAINTCASE_BLL();
        /// <summary>
        /// 工单状态
        /// </summary>
        T_WORKORDERSTATUS_BLL workOrderStatusBll = new T_WORKORDERSTATUS_BLL();
        /// <summary>
        /// 工单操作
        /// </summary>
        private T_WORKORDEROPERATION_BLL workOrderOperationBll = new T_WORKORDEROPERATION_BLL();
        /// <summary>
        /// 工单操作日志
        /// </summary>
        private T_WORKORDERLOG_BLL workOrderLogBll = new T_WORKORDERLOG_BLL();
        /// <summary>
        /// 市场部
        /// </summary>
        T_MARKETDEPARTMENT_BLL marketDepartmentBll = new T_MARKETDEPARTMENT_BLL();
        /// <summary>
        /// 经销商
        /// </summary>
        T_DEALER_BLL dealerBll = new T_DEALER_BLL();
        /// <summary>
        /// 服务部
        /// </summary>
        T_SERVICEPROVIDER_BLL serviceProviderBll = new T_SERVICEPROVIDER_BLL();
        /// <summary>
        /// 供应商
        /// </summary>
        T_SUPPLIER_BLL supplierBll = new T_SUPPLIER_BLL();
        /// <summary>
        /// 中心部门
        /// </summary>
        T_DEPARTMENT_BLL departmentbll = new T_DEPARTMENT_BLL();
        /// <summary>
        /// 操作员
        /// </summary>
        T_USER_BLL userBll = new T_USER_BLL();
        /// <summary>
        /// 数据字典
        /// </summary>
        T_DICTIONARY_BLL dictionaryBll = new T_DICTIONARY_BLL();
        /// <summary>
        /// 
        /// </summary>
        T_LOCATIONINFORMATION_BLL liBll = new T_LOCATIONINFORMATION_BLL();
        /// <summary>
        /// 
        /// </summary>
        private T_SMS_SEND_BLL smsSendBll = new T_SMS_SEND_BLL();
        #endregion


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page); 
            if (PageHelper.GetQueryString(this.Page, "PageTarget") != "Opener")
            {
                PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
                btnCancel.Attributes["onclick"] = UserLoginInfo.FilterBackUrl(btnCancel.Attributes["onclick"]);
            }
            else
            {
                PageHelper.SetClose(this.Page, btnCancel);
                btnCancel.Value = "关 闭";
            }
            PageHelper.SetPopCalender(this.Page, txtCREATETIME);
            PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIME);
            txtVISITTIME.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',errDealMode:'1'})");
            txtPROCESSINGCOMPLETEDTIME.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',errDealMode:'1'})");

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                txtGPSUrl.Text = configBll.GetCacheValueByName(this.Page, "GPSUrl");
                ddlRepairModeBinding();
                ddlRepairTypeBinding();
                ddlDISPATCHINGTYPEBinding();

                ddlPRODUCTREASONBinding();
                ddlSERVICEREASONBinding();


                ddlISVISITCOMPLATEBinding();

                ddlDEALERCODEBinding();
                ddlSTATIONCODEBinding();
                ddlVisitSituationBinding();

                ddlCOMPLAINTTYPEBinding();
                ddlSTATUSBinding();
                ddlREMINDERTIMEBinding();
                ddlHANDLINGBinding();
                ddlOBJECTTYPEBinding();
                ddlCOMPLAINTLEVELBinding();


                ddlPRODUCTSATISFACTIONBinding();
                ddlSERVICESATISFACTIONBinding();

                ddlIsCultivateBinding();
                ddlIsTotalCheckingBinding();


                pageDataInit();
            }

            LoadButton();
        }


        #region DDL  事件
        protected void ddlVCOUNTRY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVPROVINCEBinding();
            ddlVPROVINCE_SelectedIndexChanged(null, null);
            txtVEHICLELOCATION.Text = ddlVCOUNTRY.SelectedItem.Text;
        }

        protected void ddlVPROVINCE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVPREFECTURECITYBinding();
            ddlVPREFECTURECITY_SelectedIndexChanged(null, null);
            txtVEHICLELOCATION.Text = ddlVCOUNTRY.SelectedItem.Text;
            txtVEHICLELOCATION.Text +=  ddlVPROVINCE.SelectedItem.Text;
        }

        protected void ddlVPREFECTURECITY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVCITYBinding();
            ddlVCITY_SelectedIndexChanged(null, null);
            txtVEHICLELOCATION.Text = ddlVCOUNTRY.SelectedItem.Text;
            txtVEHICLELOCATION.Text +=  ddlVPROVINCE.SelectedItem.Text;
            txtVEHICLELOCATION.Text +=  ddlVPREFECTURECITY.SelectedItem.Text;
        }

        protected void ddlVCITY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVVILLAGETOWNBinding();
            txtVEHICLELOCATION.Text = ddlVCOUNTRY.SelectedItem.Text;
            txtVEHICLELOCATION.Text += ddlVPROVINCE.SelectedItem.Text;
            txtVEHICLELOCATION.Text +=  ddlVPREFECTURECITY.SelectedItem.Text;
            txtVEHICLELOCATION.Text +=  ddlVCITY.SelectedItem.Text;
        }
        #endregion
       

        #region 页面初始化数据绑定

        /// <summary>
        /// 是否全面培训
        /// DDLs the is cultivate binding.
        /// </summary>
        private void ddlIsCultivateBinding()
        {
            ddlIsCultivate.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                           DictionaryType.GetNameByKey("IsCultivate"));
            ddlIsCultivate.DataTextField = "DictValue";
            ddlIsCultivate.DataValueField = "DictKey";
            ddlIsCultivate.DataBind();
            ddlIsCultivate.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 是否全面检查
        /// DDLs the is total checking binding.
        /// </summary>
        private void ddlIsTotalCheckingBinding()
        {
            ddlIsTotalChecking.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                           DictionaryType.GetNameByKey("IsTotalChecking"));
            ddlIsTotalChecking.DataTextField = "DictValue";
            ddlIsTotalChecking.DataValueField = "DictKey";
            ddlIsTotalChecking.DataBind();
            ddlIsTotalChecking.Items.Insert(0, new ListItem("", ""));
        }

        private void ddlVisitSituationBinding()
        {
            ddlVisitSituation.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                            DictionaryType.GetNameByKey("VisitSituation"));
            ddlVisitSituation.DataTextField = "DictValue";
            ddlVisitSituation.DataValueField = "DictKey";
            ddlVisitSituation.DataBind();
            ddlVisitSituation.Items.Insert(0, new ListItem("", ""));
        }


        private void ddlISVISITCOMPLATEBinding()
        {
            ddlDISPATCHINGTYPE.Items.Clear();
            EnumHelper.FillListControl(ddlISVISITCOMPLATE, typeof(IsVisitComplate));
            ddlISVISITCOMPLATE.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        /// 服务不满意原因
        /// DDLs the SERVICEREASON binding.
        /// </summary>
        private void ddlSERVICEREASONBinding()
        {
            ddlSERVICEREASON.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                           DictionaryType.GetNameByKey("ServiceReason"));
            ddlSERVICEREASON.DataTextField = "DictValue";
            ddlSERVICEREASON.DataValueField = "DictKey";
            ddlSERVICEREASON.DataBind();
            ddlSERVICEREASON.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 产品不满意原因
        /// DDLs the PRODUCTREASON binding.
        /// </summary>
        private void ddlPRODUCTREASONBinding()
        {
            ddlPRODUCTREASON.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                            DictionaryType.GetNameByKey("ProcuctReason"));
            ddlPRODUCTREASON.DataTextField = "DictValue";
            ddlPRODUCTREASON.DataValueField = "DictKey";
            ddlPRODUCTREASON.DataBind();
            ddlPRODUCTREASON.Items.Insert(0, new ListItem("", ""));

        }


        private void ddlRepairTypeBinding()
        {

            ddlREMINDERTIME.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                            DictionaryType.GetNameByKey("RepairType"));
            ddlREMINDERTIME.DataTextField = "DictValue";
            ddlREMINDERTIME.DataValueField = "DictKey";
            ddlREMINDERTIME.DataBind();
            ddlREMINDERTIME.Items.Insert(0, new ListItem("", ""));
        }

        private void ddlRepairModeBinding()
        {

            ddlREMINDERTIME.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                            DictionaryType.GetNameByKey("RepairMode"));
            ddlREMINDERTIME.DataTextField = "DictValue";
            ddlREMINDERTIME.DataValueField = "DictKey";
            ddlREMINDERTIME.DataBind();
            ddlREMINDERTIME.Items.Insert(0, new ListItem("", ""));
        }



        /// <summary>
        /// DDLs the COUNTRY binding.
        /// </summary>
        private void ddlVCOUNTRYBinding()
        {

            ddlVCOUNTRY.DataSource = liBll.GetCacheListByParentCode(this.Page, "");
            ddlVCOUNTRY.DataTextField = "NAME";
            ddlVCOUNTRY.DataValueField = "CODE";
            ddlVCOUNTRY.DataBind();
            ddlVCOUNTRY.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the PROVINCE binding.
        /// </summary>
        private void ddlVPROVINCEBinding()
        {
            ddlVPROVINCE.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlVCOUNTRY.SelectedValue.Trim());
            ddlVPROVINCE.DataTextField = "NAME";
            ddlVPROVINCE.DataValueField = "CODE";
            ddlVPROVINCE.DataBind();
            ddlVPROVINCE.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the PREFECTURECITY binding.
        /// </summary>
        private void ddlVPREFECTURECITYBinding()
        {
            ddlVPREFECTURECITY.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlVPROVINCE.SelectedValue.Trim());
            ddlVPREFECTURECITY.DataTextField = "NAME";
            ddlVPREFECTURECITY.DataValueField = "CODE";
            ddlVPREFECTURECITY.DataBind();
            ddlVPREFECTURECITY.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the CITY binding.
        /// </summary>
        private void ddlVCITYBinding()
        {
            ddlVCITY.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlVPREFECTURECITY.SelectedValue.Trim());
            ddlVCITY.DataTextField = "NAME";
            ddlVCITY.DataValueField = "CODE";
            ddlVCITY.DataBind();
            ddlVCITY.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the VILLAGETOWN binding.
        /// </summary>
        private void ddlVVILLAGETOWNBinding()
        {
            //ddlVVILLAGETOWN.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlVCITY.SelectedValue.Trim());
            //ddlVVILLAGETOWN.DataTextField = "NAME";
            //ddlVVILLAGETOWN.DataValueField = "CODE";
            //ddlVVILLAGETOWN.DataBind();
            //ddlVVILLAGETOWN.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the DISPATCHINGTYPE binding.
        /// </summary>
        private void ddlDISPATCHINGTYPEBinding()
        {
            ddlDISPATCHINGTYPE.Items.Clear();
            EnumHelper.FillListControl(ddlDISPATCHINGTYPE, typeof(DispatchingType));
        }

        private void ddlSTATIONCODEBinding()
        {
            string businessDivisionCode = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
            var list = serviceProviderBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' ", businessDivisionCode), " SERVICEPROVIDERNAME asc ")
                           .Select(p => new
                           {
                               ServiceProviderName = p.SERVICEPROVIDERNAME,
                               ServiceProviderID = p.SERVICEPROVIDERID,
                               ServiceProviderCode = p.SERVICEPROVIDERCODE
                           });
            ddlSTATIONCODE.DataSource = list;
            ddlSTATIONCODE.DataTextField = "ServiceProviderName";
            ddlSTATIONCODE.DataValueField = "ServiceProviderCode";
            ddlSTATIONCODE.DataBind();
            ddlSTATIONCODE.Items.Insert(0, new ListItem("", ""));
        }

        private void ddlDEALERCODEBinding()
        {
            string businessDivisionCode = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
            var list = dealerBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' ", businessDivisionCode), " DEALERNAME asc ")
                           .Select(p => new
                           {
                               DealerName = p.DEALERNAME,
                               DealerID = p.DEALERID,
                               DealerCode = p.DEALERCODE
                           });
            ddlDEALERCODE.DataSource = list;
            ddlDEALERCODE.DataTextField = "DealerName";
            ddlDEALERCODE.DataValueField = "DealerCode";
            ddlDEALERCODE.DataBind();
            ddlDEALERCODE.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 状态
        /// DDLs the STATUS binding.
        /// </summary>
        private void ddlSTATUSBinding()
        {
            List<T_WORKORDERSTATUS_Entity> list = workOrderStatusBll.GetSessionList(this.Page,
                                                                                    ((int)WorkOrderType.投诉工单).ToString());

            ddlSTATUS.DataSource = list;
            ddlSTATUS.DataTextField = "OrderStatusName";
            ddlSTATUS.DataValueField = "OrderStatusID";
            ddlSTATUS.DataBind();
        }

        /// <summary>
        /// 服务满意度
        /// DDLs the SERVICESATISFACTION binding.
        /// </summary>
        private void ddlSERVICESATISFACTIONBinding()
        {
            ddlSERVICESATISFACTION.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                           DictionaryType.GetNameByKey("ServiceSatisfaction"));
            ddlSERVICESATISFACTION.DataTextField = "DictValue";
            ddlSERVICESATISFACTION.DataValueField = "DictKey";
            ddlSERVICESATISFACTION.DataBind();
            ddlSERVICESATISFACTION.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        /// 产品满意度
        /// DDLs the PRODUCTSATISFACTION binding.
        /// </summary>
        private void ddlPRODUCTSATISFACTIONBinding()
        {
            ddlPRODUCTSATISFACTION.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                           DictionaryType.GetNameByKey("ProductSatisfaction"));
            ddlPRODUCTSATISFACTION.DataTextField = "DictValue";
            ddlPRODUCTSATISFACTION.DataValueField = "DictKey";
            ddlPRODUCTSATISFACTION.DataBind();
            ddlPRODUCTSATISFACTION.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 闭环时间
        /// DDLs the REMINDERTIME binding.
        /// </summary>
        private void ddlREMINDERTIMEBinding()
        {

            ddlREMINDERTIME.DataSource = dictionaryBll.GetCacheListByType(this.Page,
                                                                            DictionaryType.GetNameByKey("ComplaintReminderTime"));
            ddlREMINDERTIME.DataTextField = "DictValue";
            ddlREMINDERTIME.DataValueField = "DictKey";
            ddlREMINDERTIME.DataBind();
            ddlREMINDERTIME.Items.Insert(0, new ListItem("无", ""));
        }

        /// <summary>
        /// 投设类型
        /// DDLs the COMPLAINTTYPE binding.
        /// </summary>
        private void ddlCOMPLAINTTYPEBinding()
        {
            ddlCOMPLAINTTYPE.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("ComplaintType"));
            ddlCOMPLAINTTYPE.DataTextField = "DictValue";
            ddlCOMPLAINTTYPE.DataValueField = "DictKey";
            ddlCOMPLAINTTYPE.DataBind();
            ddlCOMPLAINTTYPE.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 投诉类型
        /// DDLs the OBJECTTYPE binding.
        /// </summary>
        private void ddlOBJECTTYPEBinding()
        {
            ddlOBJECTTYPE.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("ComplaintObjectType"));
            ddlOBJECTTYPE.DataTextField = "DictValue";
            ddlOBJECTTYPE.DataValueField = "DictKey";
            ddlOBJECTTYPE.DataBind();
            ddlOBJECTTYPE.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 处理方式
        /// DDLs the HANDLING binding.
        /// </summary>
        private void ddlHANDLINGBinding()
        {
            ddlHANDLING.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("ComplaintHandling"));
            ddlHANDLING.DataTextField = "DictValue";
            ddlHANDLING.DataValueField = "DictKey";
            ddlHANDLING.DataBind();
            ddlHANDLING.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 投诉等级
        /// DDLs the COMPLAINTLEVEL binding.
        /// </summary>
        private void ddlCOMPLAINTLEVELBinding()
        {
            ddlCOMPLAINTLEVEL.DataSource = dictionaryBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("ComplaintLevel"));
            ddlCOMPLAINTLEVEL.DataTextField = "DictValue";
            ddlCOMPLAINTLEVEL.DataValueField = "DictKey";
            ddlCOMPLAINTLEVEL.DataBind();
            ddlCOMPLAINTLEVEL.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 派工对象
        /// DDLs the processing object type binding.
        /// </summary>
        private void ddlProcessingObjectTypeBinding()
        {
            ddlPROCESSINGOBJECTTYPE.Items.Clear();
            EnumHelper.FillListControl(ddlPROCESSINGOBJECTTYPE, typeof(UserType));
            ddlPROCESSINGOBJECTTYPE.Items.Insert(0, new ListItem("", "-1"));
        }

        /// <summary>
        /// DDLs the market department code binding.
        /// </summary>
        private void ddlPROCESSINGMARKCODEBinding()
        {
            string businessDivisionCode = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
            ddlPROCESSINGMARKCODE.Items.Clear();
            if (ddlPROCESSINGOBJECTTYPE.SelectedValue.Trim() == "-1")
            {
                ddlPROCESSINGMARKCODE.Items.Insert(0, new ListItem("", "")); 
                ddlPROCESSINGMARKCODE.Visible = false;
                return;
            }
            switch ((UserType)Enum.Parse(typeof(UserType), ddlPROCESSINGOBJECTTYPE.SelectedValue))
            {
                case UserType.中心部门:
                case UserType.供应商:
                case UserType.市场部:
                    {
                        ddlPROCESSINGMARKCODE.Visible = false;
                    }
                    break;
                case UserType.服务商:
                case UserType.经销商:
                    {
                        ddlPROCESSINGMARKCODE.Visible = true;
                        var list = marketDepartmentBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode), " MarketDepartmentNAME asc ")
                            .Select(p => new
                            {
                                Name = (p.MARKETDEPARTMENTNAME.Length > 0) ? StringHelper.GetChineseSpell(p.MARKETDEPARTMENTNAME)[0].ToString() + "-" + p.MARKETDEPARTMENTNAME : p.MARKETDEPARTMENTNAME,
                                ID = p.MARKETDEPARTMENTID,
                                Code = p.MARKETDEPARTMENTCODE
                            });
                        ddlPROCESSINGMARKCODE.DataSource = list;
                        ddlPROCESSINGMARKCODE.DataTextField = "Name";
                        ddlPROCESSINGMARKCODE.DataValueField = "Code";
                        ddlPROCESSINGMARKCODE.DataBind();
                    }
                    break;
                default:
                    {

                    }
                    break;
            }
            ddlPROCESSINGMARKCODE.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 派工部门
        /// DDLs the processing department ID binding.
        /// </summary>
        private void ddlProcessingDepartmentIDBinding()
        {
            string businessDivisionCode = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;

            ddlPROCESSINGDEPARTMENTID.Items.Clear();
            ddlPROCESSINGDEPARTMENTIDTwo.Items.Clear();
            #region MyRegion
            if (ddlPROCESSINGOBJECTTYPE.SelectedValue.Trim() == "-1")
            {
                ddlPROCESSINGDEPARTMENTID.Items.Insert(0, new ListItem("", ""));
                ddlPROCESSINGDEPARTMENTIDTwo.Items.Insert(0, new ListItem("", ""));
               
                return;
            }
            switch ((UserType)Enum.Parse(typeof(UserType), ddlPROCESSINGOBJECTTYPE.SelectedValue))
            {
                case UserType.中心部门:
                    {
                        departmentbll.FillListControl(ddlPROCESSINGDEPARTMENTID, "0", businessDivisionCode, ((int)DepartmentType.服务部门).ToString());
                        ddlPROCESSINGDEPARTMENTIDTwo.DataSource = departmentbll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and IsCenter = '{1}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode, ((int)DepartmentType.服务部门).ToString()));
                        ddlPROCESSINGDEPARTMENTIDTwo.DataTextField = "DepartmentCode";
                        ddlPROCESSINGDEPARTMENTIDTwo.DataValueField = "DepartmentID";
                        ddlPROCESSINGDEPARTMENTIDTwo.DataBind();
                    }
                    break;
                case UserType.供应商:
                    {
                        var list = supplierBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode), " SUPPLIERNAME asc ")
                                .Select(p => new
                                {
                                    SupplierName = (p.SUPPLIERNAME.Length > 0) ? StringHelper.GetChineseSpell(p.SUPPLIERNAME)[0].ToString() + "-" + p.SUPPLIERNAME : p.SUPPLIERNAME,
                                    SupplierID = p.SUPPLIERID,
                                    SupplierCode = p.SUPPLIERCODE
                                });
                        ddlPROCESSINGDEPARTMENTID.DataSource = list;
                        ddlPROCESSINGDEPARTMENTID.DataTextField = "SupplierName";
                        ddlPROCESSINGDEPARTMENTID.DataValueField = "SupplierID";
                        ddlPROCESSINGDEPARTMENTID.DataBind();
                        ddlPROCESSINGDEPARTMENTIDTwo.DataSource = list;
                        ddlPROCESSINGDEPARTMENTIDTwo.DataTextField = "SupplierCode";
                        ddlPROCESSINGDEPARTMENTIDTwo.DataValueField = "SupplierID";
                        ddlPROCESSINGDEPARTMENTIDTwo.DataBind();

                    }
                    break;
                case UserType.市场部:
                    {
                        var list = marketDepartmentBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode), " MARKETDEPARTMENTNAME asc ")
                            .Select(p => new
                            {
                                MarketDepartmentName = (p.MARKETDEPARTMENTNAME.Length > 0) ? StringHelper.GetChineseSpell(p.MARKETDEPARTMENTNAME)[0].ToString() + "-" + p.MARKETDEPARTMENTNAME : p.MARKETDEPARTMENTNAME,
                                MarketDepartmentID = p.MARKETDEPARTMENTID,
                                MarketDepartmentCode = p.MARKETDEPARTMENTCODE
                            });
                        ddlPROCESSINGDEPARTMENTID.DataSource = list;
                        ddlPROCESSINGDEPARTMENTID.DataTextField = "MarketDepartmentName";
                        ddlPROCESSINGDEPARTMENTID.DataValueField = "MarketDepartmentID";
                        ddlPROCESSINGDEPARTMENTID.DataBind();
                        ddlPROCESSINGDEPARTMENTIDTwo.DataSource = list;
                        ddlPROCESSINGDEPARTMENTIDTwo.DataTextField = "MarketDepartmentCode";
                        ddlPROCESSINGDEPARTMENTIDTwo.DataValueField = "MarketDepartmentID";
                        ddlPROCESSINGDEPARTMENTIDTwo.DataBind();
                    }
                    break;
                case UserType.服务商:
                    {
                        var list = serviceProviderBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}'  and  MARKETDEPARTMENTCODE = '{1}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode, ddlPROCESSINGMARKCODE.SelectedValue.Trim()), " SERVICEPROVIDERNAME asc ")
                            .Select(p => new
                            {
                                ServiceProviderName = (p.SERVICEPROVIDERNAME.Length > 0) ? StringHelper.GetChineseSpell(p.SERVICEPROVIDERNAME)[0].ToString() + "-" + p.SERVICEPROVIDERNAME : p.SERVICEPROVIDERNAME,
                                ServiceProviderID = p.SERVICEPROVIDERID,
                                ServiceProviderCode = p.SERVICEPROVIDERCODE
                            });
                        ddlPROCESSINGDEPARTMENTID.DataSource = list;
                        ddlPROCESSINGDEPARTMENTID.DataTextField = "ServiceProviderName";
                        ddlPROCESSINGDEPARTMENTID.DataValueField = "ServiceProviderID";
                        ddlPROCESSINGDEPARTMENTID.DataBind();
                        ddlPROCESSINGDEPARTMENTIDTwo.DataSource = list;
                        ddlPROCESSINGDEPARTMENTIDTwo.DataTextField = "ServiceProviderCode";
                        ddlPROCESSINGDEPARTMENTIDTwo.DataValueField = "ServiceProviderID";
                        ddlPROCESSINGDEPARTMENTIDTwo.DataBind();
                    }
                    break;
                case UserType.经销商:
                    {
                        var list = dealerBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}'  and  MARKETDEPARTMENTCODE = '{1}'   and Status = '" + ((int)Status.启用).ToString() + "' ", businessDivisionCode, ddlPROCESSINGMARKCODE.SelectedValue.Trim()), " DEALERNAME asc ")
                            .Select(p => new
                            {
                                DealerName = (p.DEALERNAME.Length > 0) ? StringHelper.GetChineseSpell(p.DEALERNAME)[0].ToString() + "-" + p.DEALERNAME : p.DEALERNAME,
                                DealerID = p.DEALERID,
                                DealerCode = p.DEALERCODE
                            });
                        ddlPROCESSINGDEPARTMENTID.DataSource = list;
                        ddlPROCESSINGDEPARTMENTID.DataTextField = "DealerName";
                        ddlPROCESSINGDEPARTMENTID.DataValueField = "DealerID";
                        ddlPROCESSINGDEPARTMENTID.DataBind();
                        ddlPROCESSINGDEPARTMENTIDTwo.DataSource = list;
                        ddlPROCESSINGDEPARTMENTIDTwo.DataTextField = "DealerCode";
                        ddlPROCESSINGDEPARTMENTIDTwo.DataValueField = "DealerID";
                        ddlPROCESSINGDEPARTMENTIDTwo.DataBind();
                    }
                    break;
                default:
                    {

                    }
                    break;
            } 
            #endregion
            ddlPROCESSINGDEPARTMENTID.Items.Insert(0, new ListItem("", ""));
            ddlPROCESSINGDEPARTMENTIDTwo.Items.Insert(0, new ListItem("", ""));
        }



        /// <summary>
        /// 派工人
        /// DDLs the processing user ID binding.
        /// </summary>
        private void ddlProcessingUserIDBinding()
        {
            txtProcessingUserLogin.Text = "";
            txtProcessingUserName.Text = "";
            //string conditions = " and BUSINESSDIVISIONCODE = '{0}' and USERTYPE = '{1}' and DEPARTMENTID = '{2}' ";
            //string businessDivisionCode = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;

            //List<T_USER_Entity> userList = userBll.GetList(String.Format(conditions, businessDivisionCode, ddlPROCESSINGOBJECTTYPE.SelectedValue.Trim(), ddlPROCESSINGDEPARTMENTID.SelectedValue.Trim()), " USERNAME asc ");
            //var list = userList.Select(p => new
            //{
            //    UserName = (p.USERNAME.Length > 0) ? StringHelper.GetChineseSpell(p.USERNAME)[0].ToString() + "-" + p.USERNAME : p.USERNAME,
            //    UserID = p.USERID,
            //    LoginName = p.LOGINNAME
            //});


            //ddlPROCESSINGUSERID.DataSource = list;
            //ddlPROCESSINGUSERID.DataTextField = "UserName";
            //ddlPROCESSINGUSERID.DataValueField = "UserID";
            //ddlPROCESSINGUSERID.DataBind();
            //ddlPROCESSINGUSERID.Items.Insert(0, new ListItem("", ""));
            //ddlPROCESSINGUSERIDTwo.DataSource = list;
            //ddlPROCESSINGUSERIDTwo.DataTextField = "LoginName";
            //ddlPROCESSINGUSERIDTwo.DataValueField = "UserID";
            //ddlPROCESSINGUSERIDTwo.DataBind();
            //ddlPROCESSINGUSERIDTwo.Items.Insert(0, new ListItem("", ""));

        }
        #endregion

        #region 下拉列表选择事件
        /// <summary>
        /// 派工类型选中事件
        /// Handles the SelectedIndexChanged event of the ddlProcessingObjectType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ddlProcessingObjectType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlPROCESSINGMARKCODEBinding();
            ddlProcessingDepartmentIDBinding();
            ddlProcessingUserIDBinding();
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the ddlPROCESSINGMARKCODE control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ddlPROCESSINGMARKCODE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlProcessingDepartmentIDBinding();
            ddlProcessingUserIDBinding();
        }

        /// <summary>
        /// 派工部门选中事件
        /// Handles the SelectedIndexChanged event of the ddlProcessingDepartmentID control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ddlProcessingDepartmentID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlProcessingUserIDBinding();
        }
        #endregion


        private void pageDataInit()
        {
            string _caseid = String.IsNullOrEmpty(Request.QueryString["CASEID"]) ? "" : Request.QueryString["CASEID"].Trim();

            if (!String.IsNullOrEmpty(_caseid))
            {
                T_COMPLAINTCASE_Entity entity = bll.GetEntity("CASEID", _caseid);

                ddlProcessingObjectTypeBinding();
                ddlPROCESSINGOBJECTTYPE.SelectedValue = entity.PROCESSINGOBJECTTYPE.ToString();
                ddlProcessingObjectType_SelectedIndexChanged(null, null);
                ddlPROCESSINGMARKCODEBinding();
                ddlPROCESSINGMARKCODE.SelectedValue = entity.PROCESSINGMARKCODE;
                ddlPROCESSINGMARKCODE_SelectedIndexChanged(null, null);
                ddlPROCESSINGDEPARTMENTID.SelectedValue = entity.PROCESSINGDEPARTMENTID.ToString();
                ddlProcessingDepartmentID_SelectedIndexChanged(null, null);
                //ddlPROCESSINGUSERID.SelectedValue = entity.PROCESSINGUSERID.ToString();

                ddlVCOUNTRYBinding();
                ddlVCOUNTRY.SelectedValue = entity.VCOUNTRY;
                ddlVPROVINCEBinding();
                ddlVPROVINCE.SelectedValue = entity.VPROVINCE;
                ddlVPREFECTURECITYBinding();
                ddlVPREFECTURECITY.SelectedValue = entity.VPREFECTURECITY;
                ddlVCITYBinding();
                ddlVCITY.SelectedValue = entity.VCITY;
                ddlVVILLAGETOWNBinding();
                txtVVILLAGETOWN.Text = entity.VVILLAGETOWN;


                PageHelper.SetControlValue<T_COMPLAINTCASE_Entity>(this.Page, entity);

                //PageHelper.SetControlEnabled(this.phBaseInfo, false);


                txtCOMPLETELENGTH.Text = entity.COMPLETELENGTH.HasValue ? string.Format("{0:0.0000}", entity.COMPLETELENGTH.Value) : "0";

                CaseStatus = entity.STATUS.Trim();
                ddlSTATUS.Enabled = false;
                #region 根据工单节点类型设置界面
                T_WORKORDERSTATUS_Entity workorderstatusEntity =
                                                workOrderStatusBll.GetPageItem_T_WORKORDERSTATUS_EntityByKey(this.Page, StringHelper.ToInt(entity.STATUS.Trim(), 0));
                phVisit.Visible = true;
                switch ((StatusType)Enum.Parse(typeof(StatusType), workorderstatusEntity.TYPE.ToString()))
                {
                    case StatusType.回访节点:
                        {
                            phVisit.Visible = true;
                        }
                        break;
                    case StatusType.结束节点:
                        {
                            phVisit.Visible = true;
                            btnSQReport.Visible = true;
                        }
                        break;
                    case StatusType.起始节点:
                        {

                        }
                        break;
                    case StatusType.中间节点:
                        {

                        }
                        break;
                }


                #endregion


                #region 根据派工类型设置界面
                switch ((DispatchingType)Enum.Parse(typeof(DispatchingType), entity.DISPATCHINGTYPE.ToString()))
                {
                    case DispatchingType.电话派工:
                        {
                            btnReSendSMS.Visible = false;
                            //phPhone.Visible = true;
                            phSMSOrMobile.Visible = false;
                        }
                        break;
                    case DispatchingType.手机平台派工:
                        {
                            btnReSendSMS.Visible = false;
                            //phPhone.Visible = false;
                            phSMSOrMobile.Visible = true;
                        }
                        break;
                    case DispatchingType.短信派工:
                        {
                            btnReSendSMS.Visible = true;
                            //phPhone.Visible = false;
                            phSMSOrMobile.Visible = true;
                        }
                        break;
                }
                #endregion


                #region 时间
                txtCREATETIME.Text = entity.CREATETIME.HasValue ? entity.CREATETIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                txtLASTMODIFYTIME.Text = entity.LASTMODIFYTIME.HasValue ? entity.LASTMODIFYTIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                txtDISPATCHINGTIME.Text = entity.DISPATCHINGTIME.HasValue ? entity.DISPATCHINGTIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
               
                txtVISITTIME.Text = entity.VISITTIME.HasValue ? entity.VISITTIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                txtPROCESSINGCOMPLETEDTIME.Text = entity.PROCESSINGCOMPLETEDTIME.HasValue ? entity.PROCESSINGCOMPLETEDTIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                txtPurchaseDate.Text = entity.PURCHASEDATE.HasValue ? entity.PURCHASEDATE.Value.ToString("yyyy-MM-dd") : "";
                #endregion

                txtPROCESSINGCOMPLETEDTIME.Enabled = false;
                txtDISPATCHINGTIME.Enabled = false;
                txtVISITTIME.Enabled = false;

                txtCREATEPEOPLE.Enabled = false;
                txtCREATETIME.Enabled = false;
                txtLASTMODIFYPEOPLE.Enabled = false;
                txtLASTMODIFYTIME.Enabled = false;


                string customerId = entity.CUSTOMERID;
                string taskId = "";
                string appointmentType = ((int)AppointmentReminderType.投诉工单).ToString();
                string caseId = entity.CASEID.ToString();
                string reminderUrl = String.Format("../AppointmentReminderManage/Modify.aspx?CASEID={0}&CUSTOMERID={1}&TASKID={2}&APPOINTMENTTYPE={3}", caseId, customerId, taskId, appointmentType);
                //btnReminder.Attributes.Add("Url", reminderUrl);
                PageHelper.SetOpenPage(btnReminder, reminderUrl, "预约提醒_投诉工单_" + caseId, "500", "800", "200", "200", false, false, true, false, false, true);
                btnReminder.Visible = true;
            }

            txtCASEID.ReadOnly = true;


            switch (UserLoginInfo.GetCurrentUserType())
            {
                case UserType.中心部门:
                    {

                    }
                    break;
                case UserType.市场部:
                    {

                    }
                    break;
                case UserType.供应商:
                    {

                    }
                    break;
                case UserType.经销商:
                    {
                        ddlPROCESSINGOBJECTTYPE.Enabled = false;
                        ddlPROCESSINGMARKCODE.Enabled = false;
                        ddlPROCESSINGDEPARTMENTID.Enabled = false;
                    }
                    break;
                case UserType.服务商:
                    {
                        ddlPROCESSINGOBJECTTYPE.Enabled = false;
                        ddlPROCESSINGMARKCODE.Enabled = false;
                        ddlPROCESSINGDEPARTMENTID.Enabled = false;
                    }
                    break;
            }
        }

        /// <summary>
        /// 操作按钮加载
        /// Loads the button.
        /// </summary>
        private void LoadButton()
        {
            List<T_WORKORDEROPERATION_Entity> workOrderOperationList = workOrderOperationBll.GetList(string.Format(" and CurrentStatusID = {0} and OrderTypeID = {1} ", CaseStatus, ((int)WorkOrderType.投诉工单).ToString()), " SortOrder desc, OperationID asc ");
            foreach (var entity in workOrderOperationList)
            {
                Button btn = new Button();
                btn.ID = StaticConstant.PreComplaintCase + entity.OPERATIONID.ToString();
                btn.CommandName = StaticConstant.PreComplaintCase + entity.OPERATIONID.ToString();
                btn.Text = entity.OPERATIONNAME;
                btn.CssClass = "btn";
                //if (entity.ISCONFIRM.ToString() == ((int)IsConfirm.是).ToString())
                //{
                //    btn.OnClientClick = String.Format("return confirm('您确认要执行 {0} 操作吗？');", entity.OPERATIONNAME);
                //}
                btn.Click += new EventHandler(btnOK_Click);
                phButton.Controls.Add(btn);
            }
        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            StringBuilder sbTemp = new StringBuilder();
            string strUpdateContent = "";
            if (!(sender is Button)) return;
            Button btn = sender as Button;
            string operationId = btn.CommandName.Replace(StaticConstant.PreComplaintCase, "");
            T_WORKORDEROPERATION_Entity workOrderOperationEntity =
                workOrderOperationBll.GetCache_T_WORKORDEROPERATION_EntityByKey(this.Page, StringHelper.ToInt(operationId, 0));

            string _caseid = String.IsNullOrEmpty(Request.QueryString["CASEID"]) ? "" : Request.QueryString["CASEID"].Trim();

            T_COMPLAINTCASE_Entity complaintcaseEntity = bll.GetEntity("CASEID", _caseid);

            if (complaintcaseEntity == null)
            {
                return;
            }

            if (complaintcaseEntity.STATUS.ToString() != workOrderOperationEntity.CURRENTSTATUSID.ToString())
            {
                PageHelper.ShowMessage(this.Page, "工单状态已被他人修改！", Request.RawUrl.ToString());
                return;
            }


            if ((IsUpdateInfo)Enum.Parse(typeof(IsUpdateInfo), workOrderOperationEntity.ISUPDATEINFO.ToString()) == IsUpdateInfo.是)
            {
                strUpdateContent = PageHelper.GetControlValue<T_COMPLAINTCASE_Entity>(this.Page, ref complaintcaseEntity);
                try
                {
                    if (complaintcaseEntity.PROCESSINGDEPARTMENTNAME.Trim() != ddlPROCESSINGDEPARTMENTID.SelectedItem.Text.Trim())
                    {
                        sbTemp.Append("PROCESSINGDEPARTMENTNAME:").Append(complaintcaseEntity.PROCESSINGDEPARTMENTNAME).Append("->");
                        complaintcaseEntity.PROCESSINGDEPARTMENTNAME = ddlPROCESSINGDEPARTMENTID.SelectedItem.Text;
                        sbTemp.Append(complaintcaseEntity.PROCESSINGDEPARTMENTNAME).Append("|");
                    }
                    //complaintcaseEntity.PROCESSINGDEPARTMENTNAME = ddlPROCESSINGDEPARTMENTID.SelectedItem.Text;
                }
                catch { }
                ddlPROCESSINGDEPARTMENTIDTwo.SelectedValue = ddlPROCESSINGDEPARTMENTID.SelectedValue;
                try
                {
                    if (complaintcaseEntity.PROCESSINGDEPARTMENTCODE != ddlPROCESSINGDEPARTMENTIDTwo.SelectedItem.Text)
                    {
                        sbTemp.Append("PROCESSINGDEPARTMENTCODE:").Append(complaintcaseEntity.PROCESSINGDEPARTMENTCODE).Append("->");
                        complaintcaseEntity.PROCESSINGDEPARTMENTCODE = ddlPROCESSINGDEPARTMENTIDTwo.SelectedItem.Text;
                        sbTemp.Append(complaintcaseEntity.PROCESSINGDEPARTMENTCODE).Append("|");
                    }
                    //complaintcaseEntity.PROCESSINGDEPARTMENTCODE = ddlPROCESSINGDEPARTMENTIDTwo.SelectedItem.Text;
                }
                catch { }
                if (String.IsNullOrEmpty(ddlPROCESSINGOBJECTTYPE.SelectedValue.Trim()))
                {
                    complaintcaseEntity.PROCESSINGOBJECTTYPE = null;
                }
                //ddlPROCESSINGUSERIDTwo.SelectedValue = ddlPROCESSINGUSERID.SelectedValue;
                //complaintcaseEntity.PROCESSINGLOGINNAME = ddlPROCESSINGUSERIDTwo.SelectedItem.Text;

                try
                {
                    if (ddlDEALERCODE.SelectedItem != null && complaintcaseEntity.DEALERNAME != ddlDEALERCODE.SelectedItem.Text)
                    {
                        sbTemp.Append("DEALERNAME:").Append(complaintcaseEntity.DEALERNAME).Append("->");
                        complaintcaseEntity.DEALERNAME = (ddlDEALERCODE.SelectedItem == null) ? "" : ddlDEALERCODE.SelectedItem.Text;
                        sbTemp.Append(complaintcaseEntity.DEALERNAME).Append("|");
                    }
                }
                catch { }
                try
                {
                    if (ddlSTATIONCODE.SelectedItem != null && complaintcaseEntity.STATIONNAME != ddlSTATIONCODE.SelectedItem.Text)
                    {
                        sbTemp.Append("STATIONNAME:").Append(complaintcaseEntity.STATIONNAME).Append("->");
                        complaintcaseEntity.STATIONNAME = (ddlSTATIONCODE.SelectedItem == null) ? "" : ddlSTATIONCODE.SelectedItem.Text;
                        sbTemp.Append(complaintcaseEntity.STATIONNAME).Append("|");
                    }
                }
                catch { }

                //complaintcaseEntity.DEALERNAME = ddlDEALERCODE.SelectedItem.Text;
                //complaintcaseEntity.STATIONNAME = ddlSTATIONCODE.SelectedItem.Text;

            }

            //执行相关反射操作
            MethodResult result = new MethodResult();
            workOrderOperationBll.AssociatedAction<T_COMPLAINTCASE_Entity>(workOrderOperationEntity, complaintcaseEntity, result);
            if (result.Result == MethodResult.ResultType.Failure)
            {
                PageHelper.ShowMessage(this.Page, result.Message);
                return;
            }

            complaintcaseEntity.STATUS = workOrderOperationEntity.NEXTSTATUSID.ToString();

            if (complaintcaseEntity.DISPATCHINGTIME.HasValue)
            {
                if (StringHelper.IsNumber(ddlREMINDERTIME.SelectedValue.Trim()))
                {
                    complaintcaseEntity.TODOREMINDERTIME =
                        complaintcaseEntity.DISPATCHINGTIME.Value.AddHours(StringHelper.ToInt(ddlREMINDERTIME.SelectedValue.Trim(), 0));
                }
            }
            complaintcaseEntity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
            complaintcaseEntity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
            complaintcaseEntity.LASTMODIFYTIME = DateTime.Now;

            try
            {

                long rel = bll.Update(complaintcaseEntity);
                if (rel > 0)
                {
                    #region 更新到中间表
                    if (workOrderStatusBll.GetSessionStatusType(this.Page, complaintcaseEntity.STATUS) == StatusType.结束节点)
                    {
                        try
                        {
                            CC_COMPLAINTCASE_BLL ccBll = new CC_COMPLAINTCASE_BLL();
                            CC_COMPLAINTCASE_Entity ccEntity = new CC_COMPLAINTCASE_Entity();
                            ccBll._dal.ObjectCopyTo(complaintcaseEntity, ccEntity);
                            ccBll.Delete("CASEID", complaintcaseEntity.CASEID.ToString());
                            ccBll.Insert(ccEntity);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.WriteLog("MiddleDataBase", String.Format("CaseID:{0}--Message:{1}", complaintcaseEntity.CASEID.ToString(), ex.Message));

                        }
                    }
                    #endregion

                    #region 状态日志
                    try
                    {
                        if (configBll.GetCacheValueByName(this.Page, "WorkOrderLog").ToLower() != "False")
                        {
                            T_WORKORDERLOG_Entity workOrderLogEntity = new T_WORKORDERLOG_Entity();
                            workOrderLogBll._dal.ObjectCopyTo(workOrderOperationEntity, workOrderLogEntity);
                            workOrderLogEntity.CASEID = complaintcaseEntity.CASEID;
                            workOrderLogEntity.CUSTOMERCODE = complaintcaseEntity.CUSTOMERCODE;
                            workOrderLogEntity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                            workOrderLogEntity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                            workOrderLogEntity.CREATETIME = DateTime.Now;
                            workOrderLogEntity.UPDATECONTENT = strUpdateContent + sbTemp.ToString();
                            workOrderLogBll.Insert(workOrderLogEntity);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    #endregion
                    if (workOrderOperationEntity.ISBACKLIST.ToString() == ((int)IsBackList.否).ToString())
                    {
                        ReloadPageInitData(workOrderOperationEntity);
                        PageHelper.ShowMessage(this.Page, String.Format("执行操作 {0} 成功！工单状态由 {1} 更新为 {2} ！", workOrderOperationEntity.OPERATIONNAME, workOrderOperationEntity.CURRENTSTATUSNAME, workOrderOperationEntity.NEXTSTATUSNAME));

                    }
                    else
                    {
                        string backUrl = UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this.Page));
                        PageHelper.ShowMessage(this.Page, String.Format("执行操作 {0} 成功！工单状态由 {1} 更新为 {2} ！", workOrderOperationEntity.OPERATIONNAME, workOrderOperationEntity.CURRENTSTATUSNAME, workOrderOperationEntity.NEXTSTATUSNAME), backUrl);
                    }
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, String.Format("执行操作 {0} 失败！", workOrderOperationEntity.OPERATIONNAME));
                }
            }
            catch (Exception ex)
            {
                    PageHelper.ShowMessage(this.Page, String.Format("执行操作 {0} 失败！{1}", workOrderOperationEntity.OPERATIONNAME, ex.Message));
            }
        }

        private void ReloadPageInitData(T_WORKORDEROPERATION_Entity workOrderOperationEntity)
        {

            string _caseid = String.IsNullOrEmpty(Request.QueryString["CASEID"]) ? "" : Request.QueryString["CASEID"].Trim();

            if (!String.IsNullOrEmpty(_caseid))
            {
                T_COMPLAINTCASE_Entity entity = bll.GetEntity("CASEID", _caseid);
                PageHelper.SetControlValue<T_COMPLAINTCASE_Entity>(this.Page, entity);


                CaseStatus = entity.STATUS.Trim();
                ddlSTATUS.Enabled = false;
                #region 根据工单节点类型设置界面
                T_WORKORDERSTATUS_Entity workorderstatusEntity =
                                                workOrderStatusBll.GetPageItem_T_WORKORDERSTATUS_EntityByKey(this.Page, StringHelper.ToInt(entity.STATUS.Trim(), 0));
                phVisit.Visible = true;
                switch ((StatusType)Enum.Parse(typeof(StatusType), workorderstatusEntity.TYPE.ToString()))
                {
                    case StatusType.回访节点:
                    case StatusType.结束节点:
                        {
                            phVisit.Visible = true;
                        }
                        break;
                    case StatusType.起始节点:
                        {

                        }
                        break;
                    case StatusType.中间节点:
                        {

                        }
                        break;
                }


                #endregion


                #region 根据派工类型设置界面
                switch ((DispatchingType)Enum.Parse(typeof(DispatchingType), entity.DISPATCHINGTYPE.ToString()))
                {
                    case DispatchingType.电话派工:
                        {
                            btnReSendSMS.Visible = false;
                            //phPhone.Visible = true;
                            phSMSOrMobile.Visible = false;
                        }
                        break;
                    case DispatchingType.手机平台派工:
                        {
                            btnReSendSMS.Visible = false;
                            //phPhone.Visible = false;
                            phSMSOrMobile.Visible = true;
                        }
                        break;
                    case DispatchingType.短信派工:
                        {
                            btnReSendSMS.Visible = true;
                            //phPhone.Visible = false;
                            phSMSOrMobile.Visible = true;
                        }
                        break;
                }
                #endregion


                #region 时间
                txtCREATETIME.Text = entity.CREATETIME.HasValue ? entity.CREATETIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                txtLASTMODIFYTIME.Text = entity.LASTMODIFYTIME.HasValue ? entity.LASTMODIFYTIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                txtDISPATCHINGTIME.Text = entity.DISPATCHINGTIME.HasValue ? entity.DISPATCHINGTIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";

                txtVISITTIME.Text = entity.VISITTIME.HasValue ? entity.VISITTIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                txtPROCESSINGCOMPLETEDTIME.Text = entity.PROCESSINGCOMPLETEDTIME.HasValue ? entity.PROCESSINGCOMPLETEDTIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                txtPurchaseDate.Text = entity.PURCHASEDATE.HasValue ? entity.PURCHASEDATE.Value.ToString("yyyy-MM-dd") : "";
                #endregion

                txtPROCESSINGCOMPLETEDTIME.Enabled = false;
                txtDISPATCHINGTIME.Enabled = false;
                txtVISITTIME.Enabled = false;

                txtCREATEPEOPLE.Enabled = false;
                txtCREATETIME.Enabled = false;
                txtLASTMODIFYPEOPLE.Enabled = false;
                txtLASTMODIFYTIME.Enabled = false;
            }

            txtCASEID.ReadOnly = true;
            phButton.Controls.Clear();
            LoadButton();
            return;
        }

        protected void btnReSendSMS_Click(object sender, EventArgs e)
        {
            string _caseid = String.IsNullOrEmpty(Request.QueryString["CASEID"]) ? "" : Request.QueryString["CASEID"].Trim();

            if (!String.IsNullOrEmpty(_caseid))
            {
                T_COMPLAINTCASE_Entity entity = bll.GetEntity("CASEID", _caseid);

                if (entity.DISPATCHINGTYPE.ToString() == ((int)DispatchingType.短信派工).ToString())
                {
                    T_SMS_SEND_Entity smsSendEntity = new T_SMS_SEND_Entity();
                    string bodys = string.Empty;
                    if (String.IsNullOrEmpty(entity.PROCESSINGUSERNAME.Trim()))
                    {
                        PageHelper.ShowMessage("派工人电话号码不能为空！");
                        return;
                    }
                    string[] tels = entity.PROCESSINGUSERNAME.Trim().Split(new char[] { ';', ',', '；', '，' });
                    if (tels.Length == 0)
                    {
                        PageHelper.ShowMessage("派工人电话号码不能为空");
                        return;
                    }
                    bodys = configBll.GetCacheValueByName(this.Page, "ComplaintSMSDispatchingTemplate");

                    bodys = ReplaceStringValue(entity, bodys);


                    smsSendEntity.BODYS = bodys;
                    smsSendEntity.GHID = _userLoginInfo.UserEntity.USERID.ToString();
                    smsSendEntity.BUSINESSDIVISIONCODE = entity.BUSINESSDIVISIONCODE;
                    smsSendEntity.BUSINESSDIVISIONID = entity.BUSINESSDIVISIONID;
                    smsSendEntity.BUSINESSDIVISIONNAME = entity.BUSINESSDIVISIONNAME;
                    smsSendEntity.SUBJECTS = "重发投诉派工短信:" + entity.CASEID;
                    smsSendEntity.CREATETIME = DateTime.Now;
                    smsSendEntity.UINFO = _userLoginInfo.UserEntity.USERNAME.ToString();
                    string tempTel = "";
                    foreach (var tel in tels)
                    {
                        if (tel.Length == 0)
                        {
                            continue;
                        }
                        if (tel.IndexOf('*') >= 0)
                        {
                            tempTel = tel.Substring(0, tel.IndexOf('*'));
                        }
                        if (tempTel.Length == 0)
                        {
                            continue;
                        }
                        smsSendEntity.TEL_TO = tempTel;
                        smsSendBll.Insert(smsSendEntity);
                    }
                    PageHelper.ShowMessage("重发派工短信完成！");
                }
                else
                {
                    PageHelper.ShowMessage("不是短信派工工单，不能重发派工短信！");
                    return;
                }

            }
        }

        public string ReplaceStringValue(T_COMPLAINTCASE_Entity entity, string input)
        {
            string temp = input.ToLower();
            PropertyInfo[] propertyInfos = entity.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (propertyInfo.GetValue(entity, null) != null)
                {
                    temp = temp.Replace("[" + propertyInfo.Name.ToLower() + "]", propertyInfo.GetValue(entity, null).ToString());
                }
                else
                {
                    temp = temp.Replace("[" + propertyInfo.Name.ToLower() + "]", "");
                }
            }
            return temp;
        }
    }
}
